var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        defaultProps: {
            id: 'id',             // 此项为id项的key
            children: 'children', // 此项为修改子集数据的key
            label: 'label' // 此项为修改显示数据的key
        },
        sortList:[],
        sortitem:{},//选中分类数据
        sortname:'',//新增分类
        sortType:0,//0-新增，1-修改
        divider: false,
        edit: true,
        showAll:true,
        showAdd:false,
        showEdit:false,
    },
    methods: {
        get_data:function () {
            var that=this;
            var option = {};
            option.action = "getsearcherproductlistparams";
            ns.post(option, function(succeed, data, err) {
                if (succeed) {
                    const List=data.CategoryList;
                    that.sortList = that.buildTree(List, 0, "ID", "ParentID");
                    console.log(JSON.stringify(that.sortList));
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        buildTree:function(arr, root, key, pKey) {
            var that=this;
            const temp = [];
            for (const item of arr) {
                if (Number(item[pKey]) === Number(root)) {
                    const children = that.buildTree(arr, item[key], key, pKey);
                    if (children.length > 0) item.children = children
                    temp.push(item);
                }
            }
            return temp;
        },
        //展开全部
        do_openall:function(){
            var that=this;
            that.showAll=!that.showAll;
        },
        do_opentype:function(id){
            var that=this;
            if(that.sortList.length>0){
                for (var i = 0; i < that.sortList.length; i++){
                    if(that.sortList[i].ID==id){
                        that.sortList[i].IsSelected=!that.sortList[i].IsSelected;
                    }
                }
            }
        },
        //新增商品类型
        add_type:function(item){
            var that=this;
            console.log(JSON.stringify(item));
            that.sortitem=item;
            that.sortname='';
            that.showAdd=true;
        },
        beforeadd_type:function(action, done){
            var that=this;
            if(action==='confirm'){
                 var option={};
                 option.ID=0;
                 option.ParentID=that.sortitem.ID;
                 option.Name=that.sortname;
                 option.Sort=that.sortitem.Sort;
                 option.Level=that.sortitem.Level+1;
                 option.action='saveproductcategory';
                 ns.post(option, function(succeed, data, err) {
                    if(succeed){
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();

                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                 });
                that.showAdd=false;
                done();
            }else if(action==='cancel'){
                done();
            }
        },
        //编辑商品分类
        edit_type:function (item) {
            var that=this;
            that.sortitem=item;
            that.sortname=item.Name;
            that.showEdit=true;
        },
        beforeedit_type:function(action, done){
            var that=this;
            if(action==='confirm'){
                 var option={};
                 option.ID=that.sortitem.ID;
                 option.ParentID=that.sortitem.ParentID;
                 option.Name=that.sortname;
                 option.Sort=that.sortitem.Sort;
                 option.action='saveproductcategory';
                 ns.post(option, function(succeed, data, err) {
                    if(succeed){
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();

                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                 });
                that.showEdit=false;
                done();
            }else if(action==='cancel'){
                done();
            }
        },
        //删除商品分类
        remove_type:function (id) {
            var that=this;
            var option={};
                 option.ID=id;
                 option.action='deleteproductcategory';
                 ns.post(option, function(succeed, data, err) {
                     if(succeed){
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();

                     }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                     }
                 });
        }

       
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    // toast = new auiToast();
    // actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    app.get_data();

};

